/*
The International Authority Exposure Dataset

Master Do-File
December 2022

By: 
Cédric M. Koch & Asja Riggert
WZB Berlin Social Science Center

Coverage:
213 countries, 84 IOs, 1950-2019

Suggested Citation:
Koch, C.M. & Riggert, A. (2022). The International Authority Exposure Dataset, 1950-2019, V1. Available at https://dataverse.harvard.edu/dataverse/iad 

*/


clear

*Set command directory to current folder where source datasets are stored (to be updated according to user folder structure)
cd "...\International Authority Exposure Dataset"
import excel policyscores_jan2021.xlsx, sheet("translate in core or flank") firstrow 

* Drop missing (Mistake in the Excel file)
drop if ionumber == .

*Correct wrong ionumber
replace ionumber = 3430 if ioname == "LAIA/ALADI"
replace ionumber = 4580 if ioname == "WTO"

* Extend so that i goes up until 2019
xtset ionumber year
tsfill
bysort ionumber (year) : gen toexpand = _n == _N
expand 3 if toexpand & year == 2017
bysort ionumber (year) : replace year = year[1] + _n  - 1 
drop toexpand

bysort ionum: carryforward _all, replace

sort ionumber year
save policy, replace

clear

* Import Mia datast and merge with policy data using ionumber and year
use DP_May2021.dta

sort ionumber year

merge 1:1 ionumber year using policy.dta, force

*Overview over the merging process, which ones where in policy and not in mia
tab ioname if _merge == 2 
//WIPO in MIA since 1967 in policy since 70 & ICO only in policy


replace ionumber = 4115 if ioname =="SCO"
*Change EAC ionumbers to respective ones in COW
replace ionumber = 1470 if ionumber == 1750
replace ionumber = 4120 if ionumber == 1751

drop if year < 1950
drop if _merge==2

* Save new Dataset including Policy and Mia Scores

save policy_mia, replace

*Import Authority dataset and expand it so it goes up to the year 2019
clear 

use iadv11
rename iocode ionumber 
replace ionumber = 1880 if ioname == "General Agreement on Tariffs and Trade"
replace ionumber = 2572 if ionumber == 2570
xtset ionumber year
tsfill
bysort ionumber (year) : gen toexpand = _n == _N
expand 7 if toexpand & year == 2013
bysort ionumber (year) : replace year = year[1] + _n  - 1 
drop toexpand

drop if year < 1950

save authority2, replace

*Use the Policy and Mia merged dataset to merge with the Authority data
use policy_mia
rename _merge _merge1
label variable _merge1 "Merge policy(using) and mia(master)"

sort ionumber year
merge 1:1 ionumber year using authority2.dta 
rename _merge _merge2
label variable _merge2 "Merge mia_policy(master) and authoritywzb(using)"

*Overview over the merging process, which IOs where in Authority but not in Mia_Policy
tab ioname if _merge2 == 2 

*See which IO numbers where in Mia, but not in authority
tab ioname if _merge2 == 1

save pol_mia_aut, replace

*estpost tabstat year ionumber, by(ioname) statistics( min max) 
*esttab using table2.csv, replace cell((year ionumber)) nonumber nomtitle

***Prepare COW Dataset for merging 

clear
import delimited igo_year_formatv3.csv, clear 

*Merge forrunners

// Caricom
replace ionum = 880 if ionum == 912
// CEMAC
replace ionum = 1260 if ionum == 940
// SADC
replace ionum = 4250 if ionum == 4251


// EU-variable since 1958
replace ionum = 1830 if ionum == 1653 & year < 1993
*ECSC = 1585

drop if ionum == 1653
replace ionum = 1830 if ionum == 1585 & year < 1958
drop if ionum == 1585

// Extend EAC2 from 95 to 93
expand 2 if ionum == 4120 & year == 1996, gen(new)
replace year = 1995 if ionum == 4120 & new == 1 & year == 1996
expand 2 if ionum == 4120 & year == 1995, gen(new2)
replace year = 1994 if ionum == 4120 & new == 1 & year == 1995 & new2 == 1 
expand 2 if ionum == 4120 & year == 1994, gen(new3)
replace year = 1993 if ionum == 4120 & new == 1 & year == 1994 & new3 == 1
drop new new2 new3


*Generate a germany variable that is based on Westgermany before unity
gen germany1 = germany if year >1989 
replace germany1 = wgermany if year <1990

drop wgermany egermany germany
rename germany1 germany

*Expand dataset so it runs up to 2019
bysort ionum (year) : gen toexpand = _n == _N
expand 6 if toexpand & year == 2014
bysort ionum (year) : replace year = year[1] + _n  - 1 
drop toexpand

rename accuracyofpre1965membershipdates accuracypre1965

drop if year < 1950

*Rename variables with a prefix, so the reshape command knows which variables to reshape 

foreach x of var * { 
	rename `x' v_`x' 
} 

*Reverse renaming for variables that should not be reshaped  
rename v_ionum ionum
rename v_igocode igocode
rename v_ioname ioname
rename v_year year
rename v_orgname orgname

rename v_accuracypre1965 accuracypre1965
rename v_version version
rename v_sourcesandnotes sourcesandnotes
rename v_imputed imputed
rename v_political political
rename v_social social
rename v_economic economic
rename v_longorgname longorgname
rename v_sdate sdate
rename v_deaddate deaddate
rename v_dead dead
rename v_replaced replaced
 
reshape long v_@, i(ionum year) j(country) string

sort country ionum

// generate EEA IO
expand 2 if ionum == 1830 & inrange(year,1994,2019), gen(new)
replace ionum = 1831 if new == 1 
replace ioname = "EEA" if ionum == 1831
replace orgname = "European Economic Area" if ionum == 1831
replace igocode = . if ionum == 1831
drop new

sort ioname year country
*update EEA memberships relative to EU membership, based on official EU sources cited in https://en.wikipedia.org/wiki/European_Economic_Area
replace v_ = 1 if country == "norway" & ionum == 1831 
replace v_ = 1 if country == "iceland" & ionum == 1831 
replace v_ = 1 if country == "austria" & ionum == 1831 & year==1994 // entry info force as EFTA member, ahead of EU membership
replace v_ = 0 if country == "bulgaria" & ionum == 1831 & year<2011 // entry info force separate from EU membership
replace v_ = 0 if country == "croatia" & ionum == 1831 & year<2014 // provisional application of agreement separate from ratification
replace v_ = 0 if country == "cyprus" & ionum == 1831 & year<2005 // entry into force after EU membership
replace v_ = 0 if country == "czechrep" & ionum == 1831 & year<2005 // entry into force after EU membership
replace v_ = 0 if country == "estonia" & ionum == 1831 & year<2005 // entry into force after EU membership
replace v_ = 1 if country == "finland" & ionum == 1831 & year==1994 // entry info force as EFTA member, ahead of EU membership
replace v_ = 0 if country == "estonia" & ionum == 1831 & year<2005 // entry into force after EU membership
replace v_ = 0 if country == "hungary" & ionum == 1831 & year<2005 // entry into force after EU membership
replace v_ = 0 if country == "latvia" & ionum == 1831 & year<2005 // entry into force after EU membership
replace v_ = 0 if country == "lithuania" & ionum == 1831 & year<2005 // entry into force after EU membership
replace v_ = 0 if country == "malta" & ionum == 1831 & year<2005 // entry into force after EU membership
replace v_ = 0 if country == "poland" & ionum == 1831 & year<2005 // entry into force after EU membership
replace v_ = 0 if country == "romania" & ionum == 1831 & year<2011 // entry into force after EU membership
replace v_ = 0 if country == "slovakia" & ionum == 1831 & year<2005 // entry into force after EU membership
replace v_ = 0 if country == "slovenia" & ionum == 1831 & year<2005 // entry into force after EU membership
replace v_ = 1 if country == "sweden" & ionum == 1831 & year==1994 // entry info force as EFTA member, ahead of EU membership
replace v_ = 0 if country == "malta" & ionum == 1831 & year<2005 // entry into force after EU membership

*Update IO membership, based on changes between 2015 and 2019 --> see excelfile

rename v_ mia
rename ionum ionumber
*BIS
replace mia = 1 if ionumber == 810 & country == "morocco" & year > 2019
replace mia = 1 if ionumber == 810 & country == "vietnam" & year >2019
replace mia = 1 if ionumber == 810 & country == "kuwait" & year >2019

*CERN
replace mia = 1 if ionumber == 1720 & country == "romania" & year > 2015 // Romania
replace mia = 1 if ionumber == 1720 & country == "yugoslaviaserb" & year > 2018 // Serbia
replace mia = 2 if ionumber == 1720 & country == "croatia" & year == 2019
replace mia = 2 if ionumber == 1720 & country== "lithuania" & year > 2017
replace mia = 2 if ionumber == 1720 & country == "cyprus" & year > 2015 // Cyprus
replace mia = 2 if ionumber == 1720 & country == "slovenia" & year > 2016
replace mia = 1 if ionumber == 1720 & country == "israel" & year > 2013

*CIS
replace mia = 0 if ionumber == 1320 & country == "ukraine" & year > 2017

*COMESA
replace mia = 1 if ionumber == 1170 & country == "tunisia" & year > 2017
replace mia = 1 if ionumber == 1170 & country == "somalia" & year > 2017

*EAC
replace mia = 1 if ionumber == 4120 & country == "sosudan" & year > 2015

*ECCAS
replace mia = 1 if ionumber == 1500 & country == "rwanda" & year > 2015

*ESA
replace mia = 1 if ionumber == 1790 & country == "estonia" & year > 2014
replace mia = 1 if ionumber == 1790 & country == "hungary" & year > 2014
replace mia = 2 if ionumber == 1790 & country == "slovenia" & year > 2015
replace mia = 2 if ionumber == 1790 & country == "latvia" & year > 2019
replace mia = 2 if ionumber == 1790 & country == "lithuania" & year > 2020

*GEF
replace mia = 1 if ionumber == 1900 & country == "bahrain" & year > 2019
																		  
*IAEA
replace mia = 1 if ionumber == 2370 & country == "bahamas" & year > 2013
replace mia = 1 if ionumber == 2370 & country == "brunei" & year > 2013
replace mia = 1 if ionumber == 2370 & country == "antiguabarbuda"& year > 2014
replace mia = 1 if ionumber == 2370 & country == "barbados" & year > 2014
replace mia = 1 if ionumber == 2370 & country == "djibouti" & year > 2014
replace mia = 1 if ionumber == 2370 & country == "guyana" & year > 2014
replace mia = 1 if ionumber == 2370 & country == "vanuatu" & year > 2014
replace mia = 1 if ionumber == 2370 & country == "turkmenistan" & year > 2015
replace mia = 1 if ionumber == 2370 & country == "stvincent" & year > 2016
replace mia = 1 if ionumber == 2370 & country == "grenada" & year > 2017
replace mia = 1 if ionumber == 2370 & country == "stlucia" & year > 2018
replace mia = 1 if ionumber == 2370 & country == "comoros" & year > 2019
replace mia = 1 if ionumber == 2370 & country == "wsamoa" & year > 2020

*IBRD
replace mia = 1 if ionumber == 2400 & country == "nauru" & year > 2015

*ICAO
replace mia = 1 if ionumber == 2500 & country == "dominica" & year > 2018
replace mia = 1 if ionumber == 2500 & country == "tuvalu" & year > 2016

*ICC
replace mia = 1 if ionumber == 2702 & country == "palestine" & year > 2014
replace mia = 1 if ionumber == 2702 & country == "elsalvador" & year > 2015
replace mia = 1 if ionumber == 2702 & country == "kiribati" & year > 2015

*ILO
replace mia = 1 if ionumber == 2830 & country == "cook island" & year > 2014
replace mia = 1 if ionumber == 2830 & country == "tonga" & year > 2015

*IMF
replace mia = 1 if ionumber == 2880 & country == "nauru" & year > 2015
replace mia = 1 if ionumber == 2880 & country == "andorra" & year > 2019

*IMO
replace mia = 1 if ionumber == 2860 & country == "zambia" & year > 2013
replace mia = 1 if ionumber == 2860 & country == "belarus" & year > 2015
replace mia = 1 if ionumber == 2860 & country == "armenia" & year > 2017
replace mia = 1 if ionumber == 2860 & country == "nauru" & year > 2017

*IOM
replace mia = 1 if ionumber == 2250 & country == "uzbekistan" & year > 2018
replace mia = 1 if ionumber == 2250 & country == "grenada" & year > 2017
replace mia = 1 if ionumber == 2250 & country == "laos" & year > 2017
replace mia = 1 if ionumber == 2250 & country == "palau" & year > 2017
replace mia = 1 if ionumber == 2250 & country == "cook island" & year > 2016
replace mia = 1 if ionumber == 2250 & country == "cuba" & year > 2016
replace mia = 1 if ionumber == 2250 & country == "dominica" & year > 2016
replace mia = 1 if ionumber == 2250 & country == "china" & year > 2015
replace mia = 1 if ionumber == 2250 & country == "solomonis" & year > 2015
replace mia = 1 if ionumber == 2250 & country == "tonga" & year > 2015
replace mia = 1 if ionumber == 2250 & country == "tuvalu" & year > 2015
replace mia = 1 if ionumber == 2250 & country == "fyromacedonia" & year > 2013
replace mia = 1 if ionumber == 2250 & country == "wsamoa" & year > 2013
replace mia = 1 if ionumber == 2250 & country == "eritrea" & year > 2014
replace mia = 1 if ionumber == 2250 & country == "kiribati" & year > 2014
replace mia = 1 if ionumber == 2250 & country == "stkitts" & year > 2014
replace mia = 1 if ionumber == 2250 & country == "stlucia" & year > 2014
replace mia = 1 if ionumber == 2250 & country == "saotome" & year > 2014

replace mia = 3 if ionumber == 2250 & country == "malaysia" & year > 2019
replace mia = 3 if ionumber == 2250 & country == "kuwait" & year > 2016

*ISA
replace mia = 1 if ionumber == 3100 & country == "azerbaijan" & year > 2015
replace mia = 1 if ionumber == 3100 & country == "palestine" & year > 2014

*Iwhale
replace mia = 1 if ionumber == 3250 & country == "libera" & year > 2017
replace mia = 1 if ionumber == 3250 & country == "saotome" & year > 2017

*Interpol
replace mia = 1 if ionumber == 2700 & country == "micronesiafs" & year > 2020
replace mia = 1 if ionumber == 2700 & country == "kiribati" & year > 2017
replace mia = 1 if ionumber == 2700 & country == "vanuatu" & year > 2017
replace mia = 1 if ionumber == 2700 & country == "palestine" & year > 2016
replace mia = 1 if ionumber == 2700 & country == "solomonis" & year > 2016

*Mercosur
replace mia = 1 if ionumber == 4260 & country == "venezuela" & year > 2015

*NAFO
replace mia = 1 if ionumber == 2572 & country == "uk" & year > 2019

*NATO
replace mia = 1 if ionumber == 3700 & country == "montenegro" & year > 2016
replace mia = 1 if ionumber == 3700 & country == "fyromacedonia" & year > 2019

*OAS
replace mia = 1 if ionumber == 3900 & country == "liechtenstein" & year > 2013
replace mia = 1 if ionumber == 3900 & country == "montenegro" & year > 2013
replace mia = 1 if ionumber == 3900 & country == "bangladesh" & year > 2015
replace mia = 1 if ionumber == 3900 & country == "moldova" & year > 2015

*OECD
replace mia = 1 if ionumber == 3750 & country == "latvia" & year > 2015
replace mia = 1 if ionumber == 3750 & country == "lithuania" & year > 2017
replace mia = 1 if ionumber == 3750 & country == "costarica" & year > 2020
replace mia = 1 if ionumber == 3750 & country == "colombia" & year > 2019

*OECS
replace mia = 2 if ionumber == 3770 & country == "martinque" & year > 2014
replace mia = 2 if ionumber == 3770 & country == "guadeloupe" & year > 2018

*OIF
replace mia = 3 if ionumber == 270 & country == "ireland" & year > 2017
replace mia = 3 if ionumber == 270 & country == "malta" & year > 2017
replace mia = 3 if ionumber == 270 & country == "argentina" & year > 2015
replace mia = 3 if ionumber == 270 & country == "costarica" & year > 2013
replace mia = 3 if ionumber == 270 & country == "gambia" & year > 2017
replace mia = 3 if ionumber == 270 & country == "korea" & year > 2015

replace mia = 2 if ionumber == 270 & country == "kosovo" & year > 2013
replace mia = 2 if ionumber == 270 & country == "new caledonia" & year > 2015

*OPEC
replace mia = 1 if ionumber == 3840 & country == "eqguinea" & year > 2016
replace mia = 1 if ionumber == 3840 & country == "congobrazz" & year >2017
replace mia = 1 if ionumber == 3840 & country == "gabon" & year > 2015

*PCA
replace mia = 1 if ionumber == 3940 & country == "bahamas" & year > 2015
replace mia = 1 if ionumber == 3940 & country == "djibouti" & year > 2015
replace mia = 1 if ionumber == 3940 & country == "kosovo" & year > 2015
replace mia = 1 if ionumber == 3940 & country == "palastine" & year > 2014
replace mia = 1 if ionumber == 3940 & country == "mongolia" & year > 2018
replace mia = 1 if ionumber == 3940 & country == "georgia" & year > 2014

*PIF
replace mia = 2 if ionumber == 4200 & country == "tokelau" & year > 2013

*SADC
replace mia = 1 if ionumber == 4250 & country == "comoros" & year > 2016
replace mia = 1 if ionumber == 4250 & country == "madagascar" & year > 2013

*SCO
replace mia = 1 if ionumber == 4115 & country == "india" & year > 2016
replace mia = 1 if ionumber == 4115 & country == "pakistan" & year > 2016
replace mia = 2 if ionumber == 4115 & country == "belarus" & year > 2016

//SICA Observer
replace mia = 3 if ionumber == 990 & country == "malta" & year > 2014
replace mia = 3 if ionumber == 990 & country == "yugoslaviaserb" & year > 2014
replace mia = 3 if ionumber == 990 & country == "canada" & year > 2017
replace mia = 3 if ionumber == 990 & country == "sweden" & year > 2017
replace mia = 3 if ionumber == 990 & country == "morocco" & year > 2013
replace mia = 3 if ionumber == 990 & country == "turkey" & year > 2013
replace mia = 3 if ionumber == 990 & country == "qatar" & year > 2013
replace mia = 3 if ionumber == 990 & country == "canada" & year > 2017
replace mia = 3 if ionumber == 990 & country == "bolivia" & year > 2017
replace mia = 3 if ionumber == 990 & country == "egypt" & year > 2017
replace mia = 3 if ionumber == 990 & country == "georgia" & year > 2017
replace mia = 3 if ionumber == 990 & country == "uae" & year > 2019

*SPC
replace mia = 1 if ionumber == 4190 & country == "uk" & year > 2020

*UNIDO
replace mia = 0 if ionumber == 4420 & country == "france" & year > 2013
replace mia = 0 if ionumber == 4420 & country == "portugal" & year > 2013
replace mia = 0 if ionumber == 4420 & country == "belgium" & year > 2014
replace mia = 0 if ionumber == 4420 & country == "denmark" & year > 2015
replace mia = 0 if ionumber == 4420 & country == "greece" & year > 2015
replace mia = 0 if ionumber == 4420 & country == "slovakia" & year > 2016
replace mia = 1 if ionumber == 4420 & country == "antiguabarbuda" & year >2018
replace mia = 1 if ionumber == 4420 & country == "kiribati" & year > 2015
replace mia = 1 if ionumber == 4420 & country == "marshallis" & year > 2014
replace mia = 1 if ionumber == 4420 & country == "palestine" & year > 2017

*UNESCO
replace mia = 1 if ionumber == 4410 & country == "usa" & year > 2002
replace mia = 0 if ionumber == 4410 & country == "usa" & year == 2019
replace mia = 0 if ionumber == 4410 & country == "israel" & year > 2018
replace mia = 3 if ionumber == 4410 & country == "new caledonia" & year > 2016

*UNWTO
replace mia = 0 if ionumber == 4570 & country == "australia" & year > 2016
replace mia = 0 if ionumber == 4570 & country == "norway" & year > 2014
replace mia = 0 if ionumber == 4570 & country == "canada" & year > 2011
replace mia = 1 if ionumber == 4570 & country == "antiguabarbuda" & year > 2020
replace mia = 1 if ionumber == 4570 & country == "barbados" & year > 2014
replace mia = 1 if ionumber == 4570 & country == "somalia" & year > 2016
replace mia = 1 if ionumber == 4570 & country == "wsamoa" & year > 2014
replace mia = 1 if ionumber == 4570 & country == "comoros" & year > 2016
replace mia = 1 if ionumber == 4570 & country == "palau" & year > 2018

*WCO
replace mia = 1 if ionumber == 1650 & country == "antiguabarbuda" & year >2016
replace mia = 1 if ionumber == 1650 & country == "suriname" & year > 2017
replace mia = 1 if ionumber == 1650 & country == "kosovo" & year > 2016
replace mia = 1 if ionumber == 1650 & country == "palastine" & year > 2014
replace mia = 1 if ionumber == 1650 & country == "eqguinea" & year > 2020

*WHO
replace mia = 2 if ionumber == 4550 & country == "puerto rico" & year > 2018
replace mia = 2 if ionumber == 4550 & country == "tokelau" & year > 2018

*WIPO
replace mia = 1 if ionumber == 4560 & country == "etimor" & year > 2016
replace mia = 1 if ionumber == 4560 & country == "marshallis" & year > 2016
replace mia = 1 if ionumber == 4560 & country == "solomonis" & year > 2018
replace mia = 1 if ionumber == 4560 & country == "nauru" & year > 2019

*WMO
replace mia = 1 if ionumber == 4530 & country == "andorra" & year > 2017
replace mia = 1 if ionumber == 4530 & country == "nauru" & year > 2018

*WTO
replace mia = 1 if ionumber == 4580 & country == "afghanistan" & year > 2015
replace mia = 1 if ionumber == 4580 & country == "liberia" & year > 2015
replace mia = 1 if ionumber == 4580 & country == "kazakhstan" & year > 2014
replace mia = 1 if ionumber == 4580 & country == "seychelles" & year > 2014
replace mia = 1 if ionumber == 4580 & country == "yemen" & year > 2013


*fix membership in EFTA:
replace mia = 2 if ionumber==1670 & country=="finland" & year<1986

*ssc install kountry
*Include cow country codes in the dataset
kountry country , from(other) stuck
rename _ISO3N_ iso
kountry iso, from(iso3n) to(cown)

*See which countrys where not recognized by the kountry command and include them by hand
tab country if iso == .

* Renaming based on http://www.jkarreth.net/countrycodes.html
replace _COWN_ = 345 if country == "yugoslaviaserb"
replace _COWN_ = 920 if country == "newzealand"
replace _COWN_ = 343 if country == "fyromacedonia"
replace _COWN_ = 58 if country == "antiguabarbuda" 
replace _COWN_ = 300 if country =="austriahungary" 
replace _COWN_ = 267 if country =="baden"
replace _COWN_ = 245 if country =="bavariaelect"
replace _COWN_ = 439 if country =="burkinafaso"
replace _COWN_ = 402 if country =="capeverde"
replace _COWN_ = 484 if country =="congobrazz"
replace _COWN_ = 315 if country =="czechoslovakia"
replace _COWN_ = 42 if country =="domrepublic"
replace _COWN_ = 92 if country =="elsalvador"
replace _COWN_ = 411 if country =="eqguinea"
replace _COWN_ = 860 if country =="etimor"
replace _COWN_ = 343 if country =="fyromacedonia"
replace _COWN_ = 240 if country =="hanover"
replace _COWN_ = 273 if country =="hesseelect"
replace _COWN_ = 275 if country =="hessegrand"
replace _COWN_ = 437 if country =="ivorycoast"
replace _COWN_ = 347 if country =="kosovo"
replace _COWN_ = 983 if country =="marshallis"
replace _COWN_ = 280 if country =="mecklenburg"
replace _COWN_ = 987 if country =="micronesiafs"
replace _COWN_ = 332 if country =="modena"
replace _COWN_ = 731 if country =="nokorea"
replace _COWN_ = 678 if country =="nyemen"
replace _COWN_ = 327 if country =="papalstates"
replace _COWN_ = 910 if country =="papuanewguinea"
replace _COWN_ = 335 if country =="parma"
replace _COWN_ = 331 if country =="sanmarino"
replace _COWN_ = 403 if country =="saotome"
replace _COWN_ = 670 if country =="saudiarabia"
replace _COWN_ = 269 if country =="saxony"
replace _COWN_ = 451 if country =="sierraleone"
replace _COWN_ = 560 if country =="soafrica"
replace _COWN_ = 940 if country =="solomonis"
replace _COWN_ = 626 if country =="sosudan"
replace _COWN_ = 780 if country =="srilanka"
replace _COWN_ = 60 if country =="stkitts"
replace _COWN_ = 56 if country =="stlucia"
replace _COWN_ = 57 if country =="stvincent"
replace _COWN_ = 817 if country =="svietnam"
replace _COWN_ = 680 if country =="syemen"
replace _COWN_ = 337 if country =="tuscany"
replace _COWN_ = 329 if country =="twosicilies"
replace _COWN_ = 990 if country =="wsamoa"
replace _COWN_ = 271 if country =="wurtenburg"

*Rename variables for better overview authority data Mia dataset is called "mia"
rename _COWN_ ccode
rename mia member 


sort country ionumber year 
order ccode, before(ioname)
order ionumber, after(ioname)

replace ccode=732 if country=="korea"
drop if ccode == .
*
replace country="korea" if country=="sokorea"
sort ionumber year

replace ionumber = 3760 if ioname == "AU"

save country_year_big, replace
*replace ionumber = 1830 if ionumber == 1653


*Merge Datasets based on Ionumber and the year 
clear
use country_year_big 

merge m:1 ionumber year using pol_mia_aut

*Overview over merging process, which ones where in Policy_Authority but not in COW
tab ioname if _merge == 2

*Organise data to make it look nicer  
sort country ionumber year 
order ccode, before(ioname)
order ionumber, after(ioname)

replace ccode=732 if country=="korea"
drop if ccode == .
drop if authority ==. & delegation==.

foreach var of varlist as-kg {
	rename `var' `var'_wzb
}

save final_merger_allcountries, replace

* Drop because duplicate variables
drop acronym longorgname version inception

* Drop because of focus on final variables rather than components
drop dead sdate deaddate replaced iso sourcesandnotes typeI contract ioac igocode accuracypre1965 imputed political social economic initial end DIMENSIONS ///
COMPONENTS delaccess delsuspens delconstit delbudget delcompliance delpolicy del_agenda ///
del_final poolaccess poolsuspens poolconstit poolbudget poolcompliance poolpolicy poolagenda ///
poolfinal coverage1 thirdparty1 tribunal1 binding1 nonstate1 remedy1 preliminary1 coverage2 ///
thirdparty2 tribunal2 binding2 nonstate2 remedy2 preliminary2 DS_sum1 DS_sum2 DS_sum3 ///
coverage3 thirdparty3 tribunal3 binding3 nonstate3 remedy3 preliminary3 ///
agricult competit culturea educatio developm currency_banking welfares energyco environm ///
finan_stabil_lending foreignp fisherie healthpu humanita humanrig industri justiceh migratio ///
military regionalpolicy research macroecontaxation telecomm tradecus transpor datacoll AH

*Drop merge variable
drop _merge _merge1 _merge2

*Generate comparable final authority variables
egen authMIA= rsum(delegation pooling DS_sum_st)
egen scopeMIA= rsum(core flank)
gen authority_mia= authMIA*scopeMIA
bysort country year: egen auth_sum_mia= total(authority_mia) if member==1

/*
Robustness: Normalisation does not change results
norm scopeMIA, method(mmx)
rename mmx_scopeMIA scopeMIA2
gen authority_mia2= authMIA*scopeMIA2
bysort country year: egen auth_sum_mia2= total(authority_mia2) if member==1

pwcorr authority_mia authority_mia2
tw scatter authority_mia authority_mia2, mlabel(ioname)
*/
	
rename authority authWZB
rename scope scopeWZB
gen authority_wzb= authWZB*scopeWZB
bysort country year: egen auth_sum_wzb= total(authority_wzb) if member==1

norm auth_sum_mia, method(mmx)
rename mmx_auth_sum_mia finauth_mia

norm auth_sum_wzb, method(mmx)
rename mmx_auth_sum_wzb finauth_wzb

label var finauth_mia "Exposure to International Authority incl. Scope (MIA)"
label var finauth_wzb "Exposure to International Authority incl. Scope (WZB)"

bysort year: egen meanfinauth_mia = mean(finauth_mia)
bysort year: egen meanfinauth_wzb = mean(finauth_wzb)

label var meanfinauth_mia "Mean Exposure to International Authority incl. Scope (MIA)"
label var meanfinauth_wzb "Mean Exposure to International Authority incl. Scope (WZB)"

egen countcountry = tag(country year)
bysort year: egen sumcountry = total(countcountry)
tw (connected sumcountry year), scheme(cleanplots)

drop countcountry
*Same calculations without considering IOs' policy scope:
bysort country year: egen auth2_sum_mia= total(authMIA) if member==1
bysort country year: egen auth2_sum_wzb= total(authWZB) if member==1

norm auth2_sum_mia, method(mmx)
rename mmx_auth2_sum_mia finauth2_mia

norm auth2_sum_wzb, method(mmx)
rename mmx_auth2_sum_wzb finauth2_wzb

label var finauth2_mia "Exposure to International Authority (MIA)"
label var finauth2_wzb "Exposure to International Authority (WZB)"

bysort year: egen meanfinauth2_mia = mean(finauth2_mia)
bysort year: egen meanfinauth2_wzb = mean(finauth2_wzb)

label var meanfinauth2_mia "Mean Exposure to International Authority (MIA)"
label var meanfinauth2_wzb "Mean Exposure to International Authority (WZB)"

sort year
tw (line meanfinauth_mia year) (line meanfinauth2_mia year) (line meanfinauth_wzb year) (line meanfinauth2_wzb year), scheme(cleanplots) legend(pos(6))


save IAE_Dataset_Final, replace